   use std::collections::HashMap;
   use std::fs::File;
   use std::io::{self, BufReader};
   use xml::reader::{EventReader, XmlEvent};
   use regex::Regex;

   struct MyBatisSQLConverter {
       includes: HashMap<String, String>,
       sql_statements: Vec<String>,
   }

   impl MyBatisSQLConverter {
       fn new() -> Self {
           Self {
               includes: HashMap::new(),
               sql_statements: Vec::new(),
           }
       }

       fn load_includes(&mut self) {
           // 示例：加载一些包含的 SQL 片段
           self.includes.insert("exampleInclude".to_string(), "column1, column2".to_string());
       }

       fn parse_xml(&mut self, xml_file: &str) -> io::Result<()> {
           let file = File::open(xml_file)?;
           let file = BufReader::new(file);
           let parser = EventReader::new(file);

           let mut current_sql = String::new();
           let mut in_if = false;
           let mut in_choose = false;
           let mut in_foreach = false;        


           for event in parser {
               match event.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? {
                   XmlEvent::StartElement { name, attributes, .. } => {
                       match name.local_name.as_str() {
                           "select" | "insert" | "update" | "delete" => {
                               current_sql.clear(); // 清空当前 SQL
                           }
                           "include" => {
                               if let Some(refid) = self.get_attribute_value(&attributes, "refid") {
                                   current_sql.push_str(&self.process_include(&refid));
                               }
                           }
                           "if" => {
                               in_if = true; // 进入 if 块
                           }
                           "choose" => {
                               in_choose = true; // 进入 choose 块
                           }
                           "when" => {
                               if in_choose {
                                   if let Some(test) = self.get_attribute_value(&attributes, "test") {
                                       // 假设条件为真，直接添加内容
                                       current_sql.push_str(&self.process_if(&test));
                                   }
                               }
                           }
                           "otherwise" => {
                               if in_choose {
                                   current_sql.push_str(&self.process_if(&"".to_string())); // 处理 otherwise
                               }
                           }
                           "foreach" => {
                               if let Some(collection) = self.get_attribute_value(&attributes, "collection") {
                                   current_sql.push_str(&format!("({})", collection)); // 示例处理
                                   in_foreach = true;
                               }
                           }
                           _ => {}
                       }
                   }
                   XmlEvent::Characters(text) => {
                       if in_if || in_choose || in_foreach {
                           current_sql.push_str(&text);
                       } else {
                           current_sql.push_str(&text);
                       }
                   }
                   XmlEvent::EndElement { name } => {
                       match name.local_name.as_str() {
                           "select" | "insert" | "update" | "delete" => {
                               let sql = self.replace_parameters(&current_sql);
                               self.sql_statements.push(format!("{};", sql));
                           }
                           "if" => {
                               in_if = false; // 退出 if 块
                           }
                           "choose" => {
                               in_choose = false; // 退出 choose 块
                           }
                           "foreach" => {
                               in_foreach = false; // 退出 foreach 块
                           }
                           _ => {}
                       }
                   }
                   _ => {}
               }
           }

           Ok(())
       }

       fn get_attribute_value(&self, attributes: &Vec<xml::attribute::OwnedAttribute>, attr_name: &str) -> Option<String> {
           for attr in attributes {
               if attr.name.local_name == attr_name {
                   return Some(attr.value.clone());
               }
           }
           None
       }

       fn process_include(&self, refid: &str) -> String {
           self.includes.get(refid).cloned().unwrap_or_default()
       }

       fn process_if(&self, _test: &String) -> String {
           // 这里可以根据需要返回具体内容
           " ".to_string() // 返回空字符串，表示条件为真
       }

       fn replace_parameters(&self, sql: &str) -> String {
           let re = Regex::new(r"#\{[^}]+\}").unwrap();
           re.replace_all(sql, "?").to_string()
       }

       fn get_sql_statements(&self) -> &Vec<String> {
           &self.sql_statements
       }
   }

   fn main() -> io::Result<()> {
       let mut converter = MyBatisSQLConverter::new();
       converter.load_includes(); // 加载包含的 SQL 片段
       converter.parse_xml("mybatis.xml")?;

       for sql in converter.get_sql_statements() {
           println!("{}", sql);
       }

       Ok(())
   }
   